package com.wy.animation;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.DecelerateInterpolator;
import android.widget.ImageView;
import android.widget.LinearLayout;

/**
 * Time:8/25/21
 * Author:ypf
 * Description:
 */
public class LoadingView extends LinearLayout {

    private String TAG = "LoadingView";

    private ShapeLoadingView mShapeLodingView;
    private ImageView mIndicationView;
    private Context mContext;

    private AnimatorSet mFreeFallAnimatiorSet;
    private AnimatorSet mUpThrowAnimatiorSet;
    private float mTranslationYDistance;

    private final long ANIMATOR_DURATION = 350;
    private float factor = 1.0f;

    public LoadingView(Context context) {
        this(context, null);
    }

    public LoadingView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public LoadingView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.mContext = context;
        initLayout();
    }

    private void initLayout() {

        View.inflate(mContext, R.layout.load_view, this);
        mShapeLodingView = findViewById(R.id.shapeLoadingView);
        mIndicationView = findViewById(R.id.indication);
        mIndicationView.post(new Runnable() {
            @Override
            public void run() {
                mTranslationYDistance = mIndicationView.getTop() - mShapeLodingView.getHeight() - mShapeLodingView.getHeight() / 2;
                Log.i(TAG, "mTranslationYDistance:" + mTranslationYDistance);
                initFreeFallAnimation();
            }
        });
    }

    //初始化下落动画
    private void initFreeFallAnimation() {

        // 下落动画集合
        mFreeFallAnimatiorSet = new AnimatorSet();

        // 几何图形的垂直位移动画
        ObjectAnimator freeFallTranslationAnimator = ObjectAnimator.ofFloat(mShapeLodingView,
                "translationY",
                0,
                mTranslationYDistance);

        // 定义动画的变化率。
        freeFallTranslationAnimator.setInterpolator(new AccelerateInterpolator(factor));
        // 中间阴影缩小动画
        ObjectAnimator scaleIndication = ObjectAnimator.ofFloat(mIndicationView,
                "scaleX",
                1,
                0.3f);

        mFreeFallAnimatiorSet.setDuration(ANIMATOR_DURATION);
        mFreeFallAnimatiorSet.playTogether(freeFallTranslationAnimator, scaleIndication);

        mFreeFallAnimatiorSet.addListener(new AnimatorListenerAdapter() {

            //设置动画监听器，监听该动画的开始、停止、取消、结束等状态，我们往往会用AnimtorListener适配器类来只实现我们需要的方法
            @Override
            public void onAnimationEnd(Animator animation) {
                super.onAnimationEnd(animation);
                // 下落动画结束，改变形状，然后执行上抛动画
                upThrow();
                mShapeLodingView.changeShape();
            }
        });

        //开始播放动画
        mFreeFallAnimatiorSet.start();

    }

    private void upThrow() {

        // 下落动画集合
        mUpThrowAnimatiorSet = new AnimatorSet();

        // 几何图形的垂直位移动画
        ObjectAnimator freeFallTranslationAnimator = ObjectAnimator.ofFloat(mShapeLodingView,
                "translationY",
                mTranslationYDistance,
                0);
        // 定义动画的变化率。
        freeFallTranslationAnimator.setInterpolator(new DecelerateInterpolator(factor));

        // 中间阴影缩小动画
        ObjectAnimator scaleIndication = ObjectAnimator.ofFloat(mIndicationView,
                "scaleX",
                0.3f,
                1);

        mUpThrowAnimatiorSet.setDuration(ANIMATOR_DURATION);
        mUpThrowAnimatiorSet.playTogether(freeFallTranslationAnimator, scaleIndication);

        mUpThrowAnimatiorSet.addListener(new AnimatorListenerAdapter() {

            //设置动画监听器，监听该动画的开始、停止、取消、结束等状态，我们往往会用AnimtorListener适配器类来只实现我们需要的方法
            @Override
            public void onAnimationEnd(Animator animation) {
                super.onAnimationEnd(animation);
                // 下落动画结束，改变形状，然后执行上抛动画
                initFreeFallAnimation();
                mShapeLodingView.changeShape();
            }

            @Override
            public void onAnimationStart(Animator animation) {
                super.onAnimationStart(animation);
                // 动画开始，和旋转动画一起执行
                startShapeRoteAnimator();
            }

        });

        //开始播放动画
        mUpThrowAnimatiorSet.start();

    }

    /**
     * 执行旋转动画
     */
    private void startShapeRoteAnimator() {

        ObjectAnimator roteAnimation = mShapeLodingView.getUpThrowRoteAnimation();
        roteAnimation.setDuration(1 * 1000);
        roteAnimation.setInterpolator(new DecelerateInterpolator());
        roteAnimation.start();
    }

    public void setVisibility(int visibility) {

        super.setVisibility(View.INVISIBLE);//不要再去排放和计算，少走一些系统的源码(View的绘制流程)
        //清理动画
        mShapeLodingView.clearAnimation();
        mIndicationView.clearAnimation();
        //把LoadingView从父布局移除
        ViewGroup parent = (ViewGroup) getParent();
        if (parent != null) {
            parent.removeView(this);//从父布局移除
            removeAllViews();//移除自己所有的View
        }
    }

}